<template>
  <div class="container-select-que">
    <el-dialog
      :title="dialogConfig.name"
      :visible.sync="dialogVisible"
      @close="dialogVisible = false"
      append-to-body
    >
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item label="一级内容标签" :label-width="'120px'" prop="uid">
          <el-select
            v-model="formData.tagNo"
            placeholder="请选择"
            @focus="handleFocus(1)"
          >
            <el-option
              v-for="item in optionsContent"
              :key="item.tagNo"
              :label="item.content"
              :value="item.tagNo"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" :label-width="'120px'" prop="name">
          <el-input v-model="formData.content" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="选项A" :label-width="'120px'" prop="link">
          <el-input v-model="a.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="选项B" :label-width="'120px'" prop="seqencing">
          <el-input v-model="b.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="选项C" :label-width="'120px'" prop="seqencing">
          <el-input v-model="c.content" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="选项A发布的内容"
          :label-width="'120px'"
          prop="link"
        >
          <el-input v-model="a.generateContent" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="选项B发布的内容"
          :label-width="'120px'"
          prop="link"
        >
          <el-input v-model="b.generateContent" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="选项C发布的内容"
          :label-width="'120px'"
          prop="link"
        >
          <el-input v-model="c.generateContent" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { selectQueUpdateApi, selectQueAddApi, tagListApi } from '@api/whisper'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object
    },
    request: {
      type: Object
    },
    dialogConfig: {
      type: Object
    }
  },
  data() {
    return {
      posting: false,
      formData: this.setup(this.value),
      rules: {},
      optionsContent: [],
      a: {
        sort: 1,
        content: '',
        generateContent: ''
      },
      b: {
        sort: 2,
        content: '',
        generateContent: ''
      },
      c: {
        sort: 3,
        content: '',
        generateContent: ''
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    setup() {
      if (!this.value) {
        return {
          chooseList: [],
          content: '',
          id: '',
          tagNo: ''
        }
      } else {
        const clone = deepClone(this.value)
        console.log('clone', clone)
        this.a = clone.chooseList[0]
        this.b = clone.chooseList[1]
        this.c = clone.chooseList[2]
        return clone
      }
    },

    // 内容标签
    handleFocus(val) {
      tagListApi(val).then(res => {
        if (res) {
          console.log('内容标签', res)
          this.optionsContent = res.data.list
        }
      })
    },

    handleConfirm() {
      this.posting = true
      let func = null
      if (this.formData.id) {
        func = selectQueUpdateApi
      } else {
        func = selectQueAddApi
      }
      const clone = deepClone(this.formData)
      clone.chooseList[0] = this.a
      clone.chooseList[1] = this.b
      clone.chooseList[2] = this.c
      console.log('clone', clone)
      func(clone)
        .then(res => {
          this.dialogVisible = false
          this.$emit('success') // 刷新
        })
        .catch(err => {
          this.$message('error')
        })
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.setup()
      } else {
        console.log('val', val)
        this.a.content = ''
        this.a.generateContent = ''
        this.b.content = ''
        this.b.generateContent = ''
        this.c.content = ''
        this.c.generateContent = ''
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.container-select-que {
}
</style>
